package com.waijiaojun.tpo.web;

import com.waijiaojun.tpo.entity.weixin.WeixinUserInfo;
import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.rest.RestResponse;
import com.waijiaojun.tpo.rest.RestServerException;
import com.waijiaojun.tpo.rest.dto.BindSms;
import com.waijiaojun.tpo.rest.dto.Sms;
import com.waijiaojun.tpo.service.PhoneCodeService;
import com.waijiaojun.tpo.service.weixin.WeixinUserInfoService;
import com.waijiaojun.tpo.utils.Constants;
import com.waijiaojun.tpo.utils.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * 绑定手机
 * Created by Peter on 2018/1/26.
 */
@Controller
@RequestMapping("/phone")
public class BindPhoneController {

    private static Logger logger = LoggerFactory.getLogger(BindPhoneController.class);

    @Autowired
    private PhoneCodeService phoneCodeService;

    @Autowired
    private WeixinUserInfoService weixinUserInfoService;

    // 发送过验证码
    @RequestMapping("sendCode")
    @ResponseBody
    public RestResponse sendCode(@RequestBody Sms sms) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(sms));
            // 校验参数
            if (StringUtils.isBlank(sms.getPhone())) {
                throw new RestClientException("手机号不能为空");
            }
            if (StringUtils.isBlank(sms.getArea())) {
                throw new RestClientException("区号（国家代码）不能为空");
            }
            sms.setArea(sms.getArea().trim());
            sms.setPhone(sms.getPhone().trim());

            phoneCodeService.sendCode(sms);
            restResponse = RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }

        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    // 绑定手机号
    @RequestMapping("bind")
    @ResponseBody
    public RestResponse bind(@Valid @RequestBody BindSms bindSms) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(bindSms));
            // 校验参数
            if (StringUtils.isBlank(bindSms.getPhone())) {
                throw new RestClientException("手机号不能为空");
            }
            if (StringUtils.isBlank(bindSms.getArea())) {
                throw new RestClientException("区号（国家代码）不能为空");
            }
            if (StringUtils.isBlank(bindSms.getCode())) {
                throw new RestClientException("验证码不能为空");
            }
            bindSms.setArea(bindSms.getArea().trim());
            bindSms.setPhone(bindSms.getPhone().trim());
            bindSms.setCode(bindSms.getCode().trim());

            weixinUserInfoService.bindPhone(bindSms);
            restResponse = RestResponse.getSuccessResponse();
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }

        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }
}
